from sets import Set

class SetFilter:
    
    max_between_words = 3
    
    def __init__(self, filterString):
        self.simplefilter = []
        self.indexMatching = 0
        self.wordsIgnored = 0
        self.maxBetween = 0
        self.matchingString = ""
        for wordFilter in filterString.split():
            self.simplefilter.append(wordFilter)

    
    def testMatch(self, word):
        
        if self.indexMatching == len(self.wordfilter) -1:
            if self.simplefilter[self.indexMatching].testMatch(word):
                self.indexMatching = 0
                self.indexMatching = 0
                stringToReturn =  self.matchingString + " " + word
                self.matchingString = ""
                return stringToReturn
            else:
                self.indexMatching = 0
                self.matchingString = ""
                return None
        else:
            if self.simplefilter[self.indexMatching].testMatch(word):
                self.indexMatching += 1
                self.matchingString += word + " "
            else:
                self.indexMatching = 0
            return None
   

class SimpleWordFilter:
    
    def __init__(self, simpleFilter):
        self.wordfilter = ""
    	if	simpleFilter.startswith("*") and simpleFilter.endswith("*"):
    		self.wordfilter = simpleFilter[1:len(simpleFilter)-1]
    		self.left = True
    		self.right = True
    	elif simpleFilter.startswith("*") :
    	    self.wordfilter = simpleFilter[1:len(simpleFilter)]
            self.left = True
            self.right = False
    	elif simpleFilter.endswith("*") :
    	    self.wordfilter = simpleFilter[0:len(simpleFilter)-1]
            self.left = False
            self.right = True
        else :
            self.wordfilter = simpleFilter
            self.left = False
            self.right = False
        assert len(self.wordfilter) > 0
            
    def testMatch(self, word):
            if self.left == True and self.right == True:
                return word.find(self.wordfilter)
            elif self.left == True and self.right == False:
    	           return word.endswith(self.wordfilter)
            elif self.left == False and self.right == True:
                return word.startswith(self.wordfilter)
            else:
                return (word == self.wordfilter)
